package io.louis.vmq.core;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@Service
public final class KmqBroker { // Broker+Connection

    public static final int CAPACITY = 10000;

    private final Map<String, Kmq> kmqMap = new ConcurrentHashMap<>(64);

    public void createTopic(String name){
        kmqMap.putIfAbsent(name, new Kmq(name,CAPACITY));
    }
    public Kmq findKmq(String topic) {
        return this.kmqMap.get(topic);
    }
    public Kmq findKmq(String topic,String consumerName) {
        Kmq kmq = this.kmqMap.get(topic);
        if (consumerName != null && !"".equals(consumerName)){
            kmq.addConsumer(consumerName);
        }
        return kmq;
    }

    public KmqProducer createProducer() {
        return new KmqProducer(this);
    }
    public KmqConsumer createConsumer() {
        return this.createConsumer("");
    }
    public KmqConsumer createConsumer(String consumerName) {
        return new KmqConsumer(this,consumerName);
    }
}
